<template>
  <div class="content">
    <div class="overflow-auto">
      <div class="nav">
        <el-breadcrumb separator="/" separator-class="el-icon-arrow-right">
          <el-breadcrumb-item :to="{ path: '/Client' }">客户管理</el-breadcrumb-item>
          <el-breadcrumb-item>客户详情</el-breadcrumb-item>
        </el-breadcrumb>
      </div>
      <div class="body">
        <!-- 客户资料 -->
        <div class="Basic_Information">
          <div class="main">
            <div class="info">
              <div class="top_name">
                <div> <span>{{ user_info.name }}</span>
                  <span style="margin-left: 15px;">{{ user_info.phone }}</span>
                </div>
                <div>
                  <el-button size="small" @click="actionBlacklist('加入')" v-if="user_info.status == 1">加入黑名单</el-button>
                  <el-button type="danger" size="small" @click="actionBlacklist('移除')"
                    v-if="user_info.status == 0">拉出黑名单</el-button>
                </div>
              </div>
              <div class="top_info">
                <div class="top_info_item">
                  <span>来源渠道</span>
                  <span>{{ user_info.channel_id }}</span>
                </div>
                <div class="top_info_item">
                  <span>来源方式</span>
                  <span>{{ user_info.source }}</span>
                </div>
                <div class="top_info_item">
                  <span>成为客户时间</span>
                  <span>{{ user_info.create_time }}</span>
                </div>
              </div>
              <el-divider></el-divider>
              <div class="bottom_title">
                客户资料 <el-button type="text" @click="openEditDialog">编辑</el-button>
              </div>
              <div class="bottom">
                <div class="type_box">
                  <div class="type">
                    <span class="title">姓名</span>
                    <span>{{ user_info.name }}</span>
                  </div>
                  <div class="type">
                    <span class="title">手机号</span>
                    <span>{{ user_info.phone }}</span>
                  </div>
                  <div class="type">
                    <span class="title">性别</span>
                    <span>{{ user_info.sex }}</span>
                  </div>
                  <div class="type">
                    <span class="title">生日</span>
                    <span>{{ user_info.birth }}</span>
                  </div>
                  <div class="type">
                    <span class="title">地区</span>
                    <span>{{ user_info.area }}</span>
                  </div>
                  <div class="type">
                    <span class="title">证件号</span>
                    <span>{{ user_info.card_no }}</span>
                  </div>
                  <div class="type">
                    <span class="title">邮箱</span>
                    <span>{{ user_info.email }}</span>
                  </div>
                  <div class="type">
                    <span class="title">备注</span>
                    <span>{{ user_info.remarks }}</span>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
        <div class="Basic_Information">
          <div class="main">
            <div class="info">
              <div class="top_name"> 客户标签 </div>
              <div class="addTags">
                <div class="tagList">
                  <div class="tagListItem" v-for="(item, index) in user_info.tag" :key="index">
                    {{ item }}
                    <i class="el-icon-close" @click="openDeteleDialog(item, index)"></i>
                  </div>
                  <el-button type="text" size="small" icon="el-icon-plus" @click="AddTag">添加标签</el-button>
                </div>
              </div>
            </div>
          </div>
        </div>
        <!-- 交易信息-->
        <div class="Basic_Information">
          <div class="main">
            <div class="info">
              <div class="top_name">
                <span>交易信息</span>
              </div>

              <div class="bottom">
                <div class="bottom_left">
                  <div class="type">
                    <span class="title">最近下单时间</span>
                    <span>{{ order_info.order_time }}</span>
                  </div>
                  <div class="type">
                    <span class="title">消费金额</span>
                    <span>￥{{ order_info.order_price }}</span>
                  </div>
                  <div class="type">
                    <span class="title">订单号</span>
                    <span style="color: #0486FE;">{{ order_info.order_no }}</span>
                  </div>
                  <div class="type">
                    <span class="title">订单状态</span>
                    <span>{{ user_info.name }}</span>
                  </div>
                </div>
                <div class="bottom_right">
                  <div class="bottom_right_item">
                    <div class="bottom_right_item_top">累计消费金额</div>
                    <span>￥{{ user_info.total_consume }}</span>
                  </div>
                  <div class="bottom_right_item">
                    <div class="bottom_right_item_top">累计下单数</div>
                    <span>{{ user_info.total_order_num }}</span>
                  </div>
                  <div class="bottom_right_item">
                    <div class="bottom_right_item_top">累计间夜数</div>
                    <span>{{ user_info.total_over_night }}</span>
                  </div>
                  <div class="bottom_right_item">
                    <div class="bottom_right_item_top">客单价</div>
                    <span>￥{{ user_info.user_unit_price }}</span>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
        <el-dialog title="编辑客户资料" :visible.sync="dialogVisible_edit" width="500px">
          <div>
            <el-form label-position="right" label-width="80px" :model="editForm">
              <el-form-item label="手机号">
                <el-input v-model="editForm.phone" disabled size="small"></el-input>
              </el-form-item>
              <el-form-item label="姓名">
                <el-input v-model="editForm.name" size="small"></el-input>
              </el-form-item>
              <el-form-item label="性别">
                <el-select v-model="editForm.sex" size="small" placeholder="请选择" style="width: 100%;">
                  <el-option v-for="item in SexSelect" :key="item.value" :label="item.label" :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="生日">
                <div>
                  <el-date-picker v-model="editForm.birth" size="small" style="width: 100%;" type="date"
                    format="yyyy 年 MM 月 dd 日" value-format="yyyy-MM-dd" placeholder="请选择日期">
                  </el-date-picker>
                </div>
              </el-form-item>
              <el-form-item label="地区">
                <el-cascader v-model="editForm.areaCode" size="small" style="width: 100%;" :options="provinceList"
                  placeholder="请选择省市区"></el-cascader>
              </el-form-item>
              <el-form-item label="证件类型">
                <el-select v-model="editForm.card_type" size="small" placeholder="请选择证件类型" style="width: 100%;">
                  <el-option v-for="item in id_type_options" :key="item.id" :label="item.label" :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="证件号">
                <el-input placeholder="请输入证件号" v-model="editForm.card_no" size="small"></el-input>
              </el-form-item>

              <el-form-item label="邮箱">
                <el-input v-model="editForm.email" size="small"></el-input>
              </el-form-item>
              <el-form-item label="备注">
                <el-input v-model="editForm.remarks" size="small" type="textarea"></el-input>
              </el-form-item>
            </el-form>
          </div>
          <span slot="footer" class="dialog-footer">
            <el-button @click="dialogVisible_edit = false" size="small">取 消</el-button>
            <el-button type="primary" @click="edit_true" size="small">确 定</el-button>
          </span>
        </el-dialog>
        <AddTags @update:dialogVisible_tag="handleDialogVisibleUpdate" :dialogVisible_tag="dialogVisible_tag"
          :tagList="userTag" :user_id="user_id.id.toString()" :selectTag="user_info.tag" @AddSuccess="AddSuccess">
        </AddTags>
      </div>
    </div>
  </div>
</template>

<script>
import AddTags from '../../components/AddTags/AddTags.vue'
import { PMS_user, user_blacklist } from '@/api/hotel_user'
export default {
  components: {
    AddTags
  },
  data() {
    return {
      provinceList: [],
      dialogVisible_tag: false,//添加客户标签对话框组件

      userTag: [],//客户标签列表
      user_id: this.$route.query,
      order_info: {
        order_time: '--',//最后下单时间
        order_price: '--',//下单价格
        order_no: '--',//订单号
      },
      user_info: {
        source: '--',//来源方式
        remarks: '--',//备注
        email: '--',//邮箱
        card_no: '--',//证件号
        area: '--',//地址
        birth: '--',//生日
        sex: '--',//性别
        channel_id: '--',//渠道来源
        name: '--',//姓名
        create_time: '--',//成为客户时间
        phone: '--',//手机号
        total_consume: '--',//总消费
        total_order_num: '--',//总下单数
        total_over_night: '--',//总间夜
        user_unit_price: '--',//客单价
        tag: [],//标签
      },

      is_black: '加入黑名单',
      dialogVisible_edit: false,
      // 编辑数据
      editForm: {
        name: '',
        sex: '',
        remarks: '',
        email: '',
        card_type: '',
        card_no: '',
        areaCode: []
      },
      // 性别选择器
      SexSelect: [{
        value: '1',
        label: '男'
      }, {
        value: '2',
        label: '女'
      }, {
        value: '0',
        label: '未知'
      }],
      //证件类型选择器
      id_type_options: [
        {
          id: 1,
          label: '未知',
          value: 99
        },
        {
          id: 2,
          label: '身份证',
          value: 0
        }, {
          id: 3,
          label: '护照',
          value: 1
        }, {
          id: 4,
          label: '港澳通行证',
          value: 2
        }, {
          id: 5,
          label: '台湾通行证',
          value: 3
        },
        {
          id: 6,
          label: '回乡证',
          value: 4
        },
        {
          id: 7,
          label: '台胞证',
          value: 5
        },
        {
          id: 8,
          label: '士兵证',
          value: 6
        }, {
          id: 9,
          label: '军官证',
          value: 7
        },
        {
          id: 10,
          label: '其他',
          value: 99
        },
      ],

    }
  },
  created() {
    this.getUserInfoDetails()
    this.getUserTag()
    this.getAllProvince()


  },
  methods: {

    // 移入/移出 黑名单
    actionBlacklist(action) {
      if (action == '加入') {
        this.$confirm('确认加入黑名单吗?', '提示', {
          confirmButtonText: '确认',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.blacklist()
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          });
        });
      } else {
        this.blacklist()
      }
    },
    blacklist() {
      let data = {
        user_id: this.user_id.id
      }
      console.log('移入/移出 黑名单数据：', data);
      this.my_request('supplier/hotel-pms-user/blacklist', data, 'PUT').then(res => {
        console.log(res, '移入/移出 黑名单结果');
        let { code, message } = res.data
        if (200 == code) {
          this.$message.success(message)
          this.getUserInfoDetails()
        } else {
          this.$message.error(message)
          // this.getUserInfoDetails()
        }
      })
    },

    // 获取所有省份城市
    getAllProvince() {
      this.my_request('common/area-first').then(res => {
        let {
          code,
          data
        } = res.data
        if (200 == code) {
          // console.log(res, '获取所有省份城市');
          data.forEach(item => {
            let cityList = []; // 市级
            // 获取市级
            this.getAllCity(item.code).then(cityLists => {
              cityLists.forEach(child => {

                // 获取区级
                let districtList = []; // 区级
                this.getAllCity(child.code).then(districtLists => {
                  districtLists.forEach(district => {
                    districtList.push({ // 区级
                      label: district.name,
                      value: district.name,
                    });
                  });
                });
                cityList.push({ // 市级
                  label: child.name,
                  value: child.name,
                  children: districtList
                });
              });
            });

            this.provinceList.push({
              label: item.name,
              value: item.name,
              children: cityList
            });
          });

        }
      })
      // console.log(this.provinceList, '获取所有省份城市provinceList');
    },
    getAllCity(code) {
      let data = {
        code: code
      }
      let list = []
      return this.my_request('common/area-code', data).then(res => {
        // console.log(res, '获取所有省份得市级');
        let {
          code,
          data
        } = res.data
        if (200 == code) {
          list = data
        }
        return list
      })
    },

    /**
     * 
     * @param {*} item 
     * @param {*} index 删除用户标签
     */
    openDeteleDialog(item, index) {
      console.log(item, 'item');
      this.$confirm('确定删除该标签吗？', {
        confirmButtonText: '删除',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let data = {
          user_id: this.user_id.id,
          tag: item
        }
        console.log(data,'删除标签data');
        this.my_request('supplier/hotel-pms-user/user-tag', data, 'DELETE').then(res => {
          console.log(res, '删除标签结果');
          let { code, message } = res.data
          if (200 == code) {
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
            this.getUserInfoDetails()
          } else {
            this.$message({
              type: 'error',
              message: message
            });
          }
        })

      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });

    },
    // 更新父组件的对话框状态
    handleDialogVisibleUpdate(value) {
      this.dialogVisible_tag = value;
    },
    /**
     * 添加标签成功，刷新标签列表，关闭对话框
     */
    AddSuccess() {
      this.getUserTag()//更新标签列表
      this.handleDialogVisibleUpdate()//关闭对话框
      this.getUserInfoDetails()
    },

    // 添加标签
    AddTag() {
      this.dialogVisible_tag = true
    },
    // 获取客户标签列表
    getUserTag() {
      let data = {
        hotel_id: sessionStorage.getItem('hotel_id'),
        page: 1,
        page_rows: 1000,
      }
      this.my_request('supplier/hotel-pms-user/tag', data, 'GET').then(res => {
        console.log(res, '获取客户标签列表');
        let { code, data } = res.data
        if (200 == code) {
          this.userTag = data.list.map(item => {
            item.value = item.name
            return item
          })
        }
      })
    },

    // 打开编辑对话款
    openEditDialog() {
      this.dialogVisible_edit = true
      this.editForm = JSON.parse(JSON.stringify(this.user_info ? this.user_info : []))
      this.editForm.birth = this.editForm.birth == '未知' ? '' : this.editForm.birth//生日
      this.SexSelect.forEach(item => {
        if (item.label == this.editForm.sex) {
          this.editForm.sex = item.value
        }
      })
      console.log(this.editForm, 'this.editForm ');


    },
    // 确认编辑客户
    edit_true() {
      let data = {
        user_id: this.user_id.id,//客户id 
        sex: this.editForm.sex,//性别
        birth: this.editForm.birth,//生日
        remarks: this.editForm.remarks,//备注
        province_id: this.editForm.areaCode ? this.editForm.areaCode[0] : 0, //省级id 
        city_id: this.editForm.areaCode ? this.editForm.areaCode[1] : 0,//市级id 
        area_id: this.editForm.areaCode ? this.editForm.areaCode[2] : 0,//区级id 
        card_type: this.editForm.card_type,//证件类型
        card_no: this.editForm.card_no,//证件号
        email: this.editForm.email,//邮箱
      }
      console.log(data, '确认编辑data');
      this.my_request('supplier/hotel-pms-user', data, 'PUT').then(res => {
        console.log(res, '确认编辑结果');
        let { code, data, message } = res.data
        if (200 == code) {
          this.$message({
            message: '编辑成功',
            type: "success"
          })
          this.dialogVisible_edit = false
          this.getUserInfoDetails()
        } else {
          this.$message({
            message: message,
            type: "error"
          })
        }
      })
    },



    // 获取客户详情
    async getUserInfoDetails() {
      this.user_id = this.$route.query
      let data = {
        user_id: this.user_id.id,
        hotel_id: sessionStorage.getItem('hotel_id')
      }
      await PMS_user(data, 'GET').then(res => {
        console.log(res, '获取客户详情结果');
        this.user_info = res.data.data.user_info
        this.order_info = res.data.data.order_info
        console.log(this.user_info, "user_info");
        console.log(this.order_info, "order_info");
      })
    },


  }

}
</script>

<style lang="scss" scoped>
.el-form-item {
  margin-bottom: 10px;
}

.addTags {
  .tagList {
    display: flex;
    flex-wrap: wrap;
    margin-top: 20px;
    align-items: center;

    .el-button {
      margin-top: -10px;
    }

    .tagListItem {
      background-color: #EFEFEF;
      padding: 2px 5px;
      font-size: 14px;
      margin: 0 10px 10px 0;
      // height: 20px;
    }
  }
}

.content {
  padding: 20px;

  .overflow-auto {
    display: flex;
    overflow: auto;
    position: absolute;
    flex-direction: column;
  }

  .nav {
    width: 100%;
  }

  .body {
    .Basic_Information {
      .main {
        .top_name {
          display: flex;
          justify-content: space-between;
          align-items: center;
        }

        .top_info {
          width: calc(100% - 40px);
          height: 50px;
          padding: 15px 20px;
          margin-top: 20px;
          display: flex;
          background-color: #e4e7ed;

          .top_info_item {
            width: 25%;

            span {
              display: block;
              line-height: 25px;
            }

            span:nth-child(1) {
              color: #8b8b8b;
              font-size: 13px;
            }
          }
        }

        .info {
          padding: 15px 25px;
          width: calc(100% - 50px);
          background-color: white;
          border: 1px solid #e4e7ed;
          margin-top: 20px;

          .bottom {
            // padding: 0 1%;
            display: flex;

            .type_box {
              display: flex;
              flex-wrap: wrap;
              margin: 0 1%;
              width: 100%;
            }

            .type {
              width: 24%;
              margin-bottom: 20px;
              color: #333333;

              span:nth-child(2) {
                margin-left: 20px;
              }
            }
          }

          .bottom_left {
            padding: 15px;
            background-color: #F2F3F6;
            width: 50%;
            margin-top: 10px !important;
            height: 120px;

            .type {
              width: 100%;
              margin-bottom: 10px !important;

              span:nth-child(1) {
                text-align: right;
                display: inline-block;
                width: 6em;
                // color: #b9b9ba;
              }

              span:nth-child(2) {
                margin-left: 10px !important;
              }
            }
          }

          .bottom_right {
            margin-top: 10px !important;
            width: 50%;
            height: 0 20px;
            display: flex;
            flex-wrap: wrap;
            justify-content: space-around;
            // margin-right: 2%;
            padding: 0 20px;

            .bottom_right_item {
              width: 48%;
              height: 50%;

              div {
                font-size: 12px;
              }

              span {
                font-size: 20px;
                font-weight: 500;
                line-height: 40px;
              }
            }
          }
        }
      }
    }
  }
}
</style>